//
//  ContactListController.m
//  MClient
//
//  Created by zhouyi on 11-6-7.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RarOrZipController.h"
#import "MServerDAO.h"
#import "MCUIToolkit.h"
#import "MCFunction.h"

@implementation RarOrZipController

- (void)viewDidLoad {
	[super viewDidLoad];

	self.tableview.frame = CGRectMake(0, 0, self.width, self.height);
	//self.tableview.backgroundColor = [UIColor clearColor];
}

- (void)getDataFromServer:(NSString*)dataFrom
				 fileParam:(NSMutableDictionary *)fileParam {
	_dataFrom = dataFrom;
	lcn = [fileParam objectForKey:@"lcn"];
	
	[[MServerDAO sharedInstance] getFileList:dataFrom
								fileParam:fileParam 
									delegate:self];
}

- (void)dealloc {
	[fileInfos release];
	
    [super dealloc];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [fileInfos count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSString *identifier = [NSString stringWithFormat:@"RarOrZipTableCell-%d-%d", indexPath.section, indexPath.row];
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
	if (nil == cell) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
	}
	cell.accessoryType = UITableViewCellAccessoryNone;
	
	NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:indexPath.row];
	NSString *fname = [fileInfo objectForKey:@"fname"]; 
	NSString *size = [fileInfo objectForKey:@"size"]; 
	
	UILabel *fnameLabel = (UILabel *)[cell.contentView viewWithTag:1];
	if (nil == fnameLabel) {
		fnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 6, 200, 32)];
		fnameLabel.tag = 1;
		fnameLabel.font = [UIFont boldSystemFontOfSize:18];
		[cell.contentView addSubview:fnameLabel];
		[fnameLabel release];
	}
	fnameLabel.text = fname;
	
	UILabel *sizeLabel = (UILabel *)[cell.contentView viewWithTag:2];
	if (nil == sizeLabel) {
		sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width - 90, 6, 80, 32)];
		sizeLabel.tag = 2;
		sizeLabel.font = [UIFont systemFontOfSize:16];
		[cell.contentView addSubview:sizeLabel];
		[sizeLabel release];
	}
	sizeLabel.text = size;
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	cell.accessoryType = UITableViewCellAccessoryNone;
	
	NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:indexPath.row];
	NSMutableDictionary *newFileInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
									  [fileInfo objectForKey:@"lcn"], @"fname",
									  lcn, @"lcn", nil];
	[MCFunction filePreview:self
				   dataFrom:_dataFrom 
				   fileInfo:newFileInfo];
	[newFileInfo release];
}

#pragma mark ---- 事件处理方法 ----

- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	if (fileInfos) {
		[fileInfos release]; fileInfos = nil;
	}
	
	fileInfos = (NSMutableArray *)returnValue;
	[fileInfos retain];
	[self.tableview reloadData];
} 

@end
